/*
Source : https://leetcode.com/problems/binary-tree-preorder-traversal/
Author : nflush@outlook.com
Date   : 2016-07-05
*/

/*
Total Accepted: 128707 Total Submissions: 316285 Difficulty: Medium

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

Subscribe to see which companies asked this question
*/
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    void insertNode(vector<int> &ret, TreeNode* tr){
        if (tr){
            ret.push_back(tr->val);
            insertNode(ret, tr->left);
            insertNode(ret, tr->right);
        }
    }
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ret;
        insertNode(ret, root);
        return ret;
    }
};
